{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2ad1a59d",
   "metadata": {},
   "source": [
    "# 元组(tuple)\n",
    "\n",
    "* [元组]()\n",
    "> 1. 它是和list几乎完全一样的序列数据\n",
    "> 2. 它与list不同的地方是，**不可变** （因为不可变，所以几乎没有方法）\n",
    "> 3. 元组的形式： （1，2，3，4，5） 对比 元组（） 列表[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2f0cff95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('10001', '10002', '10003', '10001') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "stu_id = \"10001\",\"10002\",\"10003\",\"10001\"\n",
    "print(stu_id,type(stu_id))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4786be98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id.count(\"10001\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ae013e8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id.index(\"10002\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b91d2db0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0d8066a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=27, tm_hour=22, tm_min=31, tm_sec=47, tm_wday=2, tm_yday=300, tm_isdst=0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e6cef727",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in module time:\n",
      "\n",
      "NAME\n",
      "    time - This module provides various functions to manipulate time values.\n",
      "\n",
      "DESCRIPTION\n",
      "    There are two standard representations of time.  One is the number\n",
      "    of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer\n",
      "    or a floating point number (to represent fractions of seconds).\n",
      "    The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n",
      "    The actual value can be retrieved by calling gmtime(0).\n",
      "    \n",
      "    The other representation is a tuple of 9 integers giving local time.\n",
      "    The tuple items are:\n",
      "      year (including century, e.g. 1998)\n",
      "      month (1-12)\n",
      "      day (1-31)\n",
      "      hours (0-23)\n",
      "      minutes (0-59)\n",
      "      seconds (0-59)\n",
      "      weekday (0-6, Monday is 0)\n",
      "      Julian day (day in the year, 1-366)\n",
      "      DST (Daylight Savings Time) flag (-1, 0 or 1)\n",
      "    If the DST flag is 0, the time is given in the regular time zone;\n",
      "    if it is 1, the time is given in the DST time zone;\n",
      "    if it is -1, mktime() should guess based on the date and time.\n",
      "\n",
      "CLASSES\n",
      "    builtins.tuple(builtins.object)\n",
      "        struct_time\n",
      "    \n",
      "    class struct_time(builtins.tuple)\n",
      "     |  struct_time(iterable=(), /)\n",
      "     |  \n",
      "     |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      "     |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      "     |  sequence of 9 integers.\n",
      "     |  \n",
      "     |  Note that several fields' values are not the same as those defined by\n",
      "     |  the C language standard for struct tm.  For example, the value of the\n",
      "     |  field tm_year is the actual year, not year - 1900.  See individual\n",
      "     |  fields' descriptions for details.\n",
      "     |  \n",
      "     |  Method resolution order:\n",
      "     |      struct_time\n",
      "     |      builtins.tuple\n",
      "     |      builtins.object\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  __reduce__(...)\n",
      "     |      Helper for pickle.\n",
      "     |  \n",
      "     |  __repr__(self, /)\n",
      "     |      Return repr(self).\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Static methods defined here:\n",
      "     |  \n",
      "     |  __new__(*args, **kwargs) from builtins.type\n",
      "     |      Create and return a new object.  See help(type) for accurate signature.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors defined here:\n",
      "     |  \n",
      "     |  tm_gmtoff\n",
      "     |      offset from UTC in seconds\n",
      "     |  \n",
      "     |  tm_hour\n",
      "     |      hours, range [0, 23]\n",
      "     |  \n",
      "     |  tm_isdst\n",
      "     |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      "     |  \n",
      "     |  tm_mday\n",
      "     |      day of month, range [1, 31]\n",
      "     |  \n",
      "     |  tm_min\n",
      "     |      minutes, range [0, 59]\n",
      "     |  \n",
      "     |  tm_mon\n",
      "     |      month of year, range [1, 12]\n",
      "     |  \n",
      "     |  tm_sec\n",
      "     |      seconds, range [0, 61])\n",
      "     |  \n",
      "     |  tm_wday\n",
      "     |      day of week, range [0, 6], Monday is 0\n",
      "     |  \n",
      "     |  tm_yday\n",
      "     |      day of year, range [1, 366]\n",
      "     |  \n",
      "     |  tm_year\n",
      "     |      year, for example, 1993\n",
      "     |  \n",
      "     |  tm_zone\n",
      "     |      abbreviation of timezone name\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data and other attributes defined here:\n",
      "     |  \n",
      "     |  n_fields = 11\n",
      "     |  \n",
      "     |  n_sequence_fields = 9\n",
      "     |  \n",
      "     |  n_unnamed_fields = 0\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from builtins.tuple:\n",
      "     |  \n",
      "     |  __add__(self, value, /)\n",
      "     |      Return self+value.\n",
      "     |  \n",
      "     |  __contains__(self, key, /)\n",
      "     |      Return key in self.\n",
      "     |  \n",
      "     |  __eq__(self, value, /)\n",
      "     |      Return self==value.\n",
      "     |  \n",
      "     |  __ge__(self, value, /)\n",
      "     |      Return self>=value.\n",
      "     |  \n",
      "     |  __getattribute__(self, name, /)\n",
      "     |      Return getattr(self, name).\n",
      "     |  \n",
      "     |  __getitem__(self, key, /)\n",
      "     |      Return self[key].\n",
      "     |  \n",
      "     |  __getnewargs__(self, /)\n",
      "     |  \n",
      "     |  __gt__(self, value, /)\n",
      "     |      Return self>value.\n",
      "     |  \n",
      "     |  __hash__(self, /)\n",
      "     |      Return hash(self).\n",
      "     |  \n",
      "     |  __iter__(self, /)\n",
      "     |      Implement iter(self).\n",
      "     |  \n",
      "     |  __le__(self, value, /)\n",
      "     |      Return self<=value.\n",
      "     |  \n",
      "     |  __len__(self, /)\n",
      "     |      Return len(self).\n",
      "     |  \n",
      "     |  __lt__(self, value, /)\n",
      "     |      Return self<value.\n",
      "     |  \n",
      "     |  __mul__(self, value, /)\n",
      "     |      Return self*value.\n",
      "     |  \n",
      "     |  __ne__(self, value, /)\n",
      "     |      Return self!=value.\n",
      "     |  \n",
      "     |  __rmul__(self, value, /)\n",
      "     |      Return value*self.\n",
      "     |  \n",
      "     |  count(self, value, /)\n",
      "     |      Return number of occurrences of value.\n",
      "     |  \n",
      "     |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      "     |      Return first index of value.\n",
      "     |      \n",
      "     |      Raises ValueError if the value is not present.\n",
      "\n",
      "FUNCTIONS\n",
      "    asctime(...)\n",
      "        asctime([tuple]) -> string\n",
      "        \n",
      "        Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n",
      "        When the time tuple is not present, current time as returned by localtime()\n",
      "        is used.\n",
      "    \n",
      "    ctime(...)\n",
      "        ctime(seconds) -> string\n",
      "        \n",
      "        Convert a time in seconds since the Epoch to a string in local time.\n",
      "        This is equivalent to asctime(localtime(seconds)). When the time tuple is\n",
      "        not present, current time as returned by localtime() is used.\n",
      "    \n",
      "    get_clock_info(...)\n",
      "        get_clock_info(name: str) -> dict\n",
      "        \n",
      "        Get information of the specified clock.\n",
      "    \n",
      "    gmtime(...)\n",
      "        gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n",
      "                               tm_sec, tm_wday, tm_yday, tm_isdst)\n",
      "        \n",
      "        Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n",
      "        GMT).  When 'seconds' is not passed in, convert the current time instead.\n",
      "        \n",
      "        If the platform supports the tm_gmtoff and tm_zone, they are available as\n",
      "        attributes only.\n",
      "    \n",
      "    localtime(...)\n",
      "        localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n",
      "                                  tm_sec,tm_wday,tm_yday,tm_isdst)\n",
      "        \n",
      "        Convert seconds since the Epoch to a time tuple expressing local time.\n",
      "        When 'seconds' is not passed in, convert the current time instead.\n",
      "    \n",
      "    mktime(...)\n",
      "        mktime(tuple) -> floating point number\n",
      "        \n",
      "        Convert a time tuple in local time to seconds since the Epoch.\n",
      "        Note that mktime(gmtime(0)) will not generally return zero for most\n",
      "        time zones; instead the returned value will either be equal to that\n",
      "        of the timezone or altzone attributes on the time module.\n",
      "    \n",
      "    monotonic(...)\n",
      "        monotonic() -> float\n",
      "        \n",
      "        Monotonic clock, cannot go backward.\n",
      "    \n",
      "    monotonic_ns(...)\n",
      "        monotonic_ns() -> int\n",
      "        \n",
      "        Monotonic clock, cannot go backward, as nanoseconds.\n",
      "    \n",
      "    perf_counter(...)\n",
      "        perf_counter() -> float\n",
      "        \n",
      "        Performance counter for benchmarking.\n",
      "    \n",
      "    perf_counter_ns(...)\n",
      "        perf_counter_ns() -> int\n",
      "        \n",
      "        Performance counter for benchmarking as nanoseconds.\n",
      "    \n",
      "    process_time(...)\n",
      "        process_time() -> float\n",
      "        \n",
      "        Process time for profiling: sum of the kernel and user-space CPU time.\n",
      "    \n",
      "    process_time_ns(...)\n",
      "        process_time() -> int\n",
      "        \n",
      "        Process time for profiling as nanoseconds:\n",
      "        sum of the kernel and user-space CPU time.\n",
      "    \n",
      "    sleep(...)\n",
      "        sleep(seconds)\n",
      "        \n",
      "        Delay execution for a given number of seconds.  The argument may be\n",
      "        a floating point number for subsecond precision.\n",
      "    \n",
      "    strftime(...)\n",
      "        strftime(format[, tuple]) -> string\n",
      "        \n",
      "        Convert a time tuple to a string according to a format specification.\n",
      "        See the library reference manual for formatting codes. When the time tuple\n",
      "        is not present, current time as returned by localtime() is used.\n",
      "        \n",
      "        Commonly used format codes:\n",
      "        \n",
      "        %Y  Year with century as a decimal number.\n",
      "        %m  Month as a decimal number [01,12].\n",
      "        %d  Day of the month as a decimal number [01,31].\n",
      "        %H  Hour (24-hour clock) as a decimal number [00,23].\n",
      "        %M  Minute as a decimal number [00,59].\n",
      "        %S  Second as a decimal number [00,61].\n",
      "        %z  Time zone offset from UTC.\n",
      "        %a  Locale's abbreviated weekday name.\n",
      "        %A  Locale's full weekday name.\n",
      "        %b  Locale's abbreviated month name.\n",
      "        %B  Locale's full month name.\n",
      "        %c  Locale's appropriate date and time representation.\n",
      "        %I  Hour (12-hour clock) as a decimal number [01,12].\n",
      "        %p  Locale's equivalent of either AM or PM.\n",
      "        \n",
      "        Other codes may be available on your platform.  See documentation for\n",
      "        the C library strftime function.\n",
      "    \n",
      "    strptime(...)\n",
      "        strptime(string, format) -> struct_time\n",
      "        \n",
      "        Parse a string to a time tuple according to a format specification.\n",
      "        See the library reference manual for formatting codes (same as\n",
      "        strftime()).\n",
      "        \n",
      "        Commonly used format codes:\n",
      "        \n",
      "        %Y  Year with century as a decimal number.\n",
      "        %m  Month as a decimal number [01,12].\n",
      "        %d  Day of the month as a decimal number [01,31].\n",
      "        %H  Hour (24-hour clock) as a decimal number [00,23].\n",
      "        %M  Minute as a decimal number [00,59].\n",
      "        %S  Second as a decimal number [00,61].\n",
      "        %z  Time zone offset from UTC.\n",
      "        %a  Locale's abbreviated weekday name.\n",
      "        %A  Locale's full weekday name.\n",
      "        %b  Locale's abbreviated month name.\n",
      "        %B  Locale's full month name.\n",
      "        %c  Locale's appropriate date and time representation.\n",
      "        %I  Hour (12-hour clock) as a decimal number [01,12].\n",
      "        %p  Locale's equivalent of either AM or PM.\n",
      "        \n",
      "        Other codes may be available on your platform.  See documentation for\n",
      "        the C library strftime function.\n",
      "    \n",
      "    thread_time(...)\n",
      "        thread_time() -> float\n",
      "        \n",
      "        Thread time for profiling: sum of the kernel and user-space CPU time.\n",
      "    \n",
      "    thread_time_ns(...)\n",
      "        thread_time() -> int\n",
      "        \n",
      "        Thread time for profiling as nanoseconds:\n",
      "        sum of the kernel and user-space CPU time.\n",
      "    \n",
      "    time(...)\n",
      "        time() -> floating point number\n",
      "        \n",
      "        Return the current time in seconds since the Epoch.\n",
      "        Fractions of a second may be present if the system clock provides them.\n",
      "    \n",
      "    time_ns(...)\n",
      "        time_ns() -> int\n",
      "        \n",
      "        Return the current time in nanoseconds since the Epoch.\n",
      "\n",
      "DATA\n",
      "    altzone = -32400\n",
      "    daylight = 0\n",
      "    timezone = -28800\n",
      "    tzname = ('中国标准时间', '中国夏令时')\n",
      "\n",
      "FILE\n",
      "    (built-in)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "410688cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = 12345, 54321, 'hello!'\n",
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "72297e96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321, 'hello!')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9e3481c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Tuples may be nested:\n",
    "... u = t, (1, 2, 3, 4, 5)\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3678f537",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3], [3, 2, 1])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Tuples are immutable:\n",
    "# but they can contain mutable objects:\n",
    "v = ([1, 2, 3], [3, 2, 1])\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "aedcb21f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empty = ()\n",
    "singleton = 'hello',    # <-- note trailing comma\n",
    "len(empty)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "714f14ce",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(singleton)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ca7aec74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('hello',)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "singleton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6d4cda7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python')\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0b7d89ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fc32b280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = ('Python',)   # trailing comma makes all the difference, as it tells the interpreter that this is a tuple\n",
    "type(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cb52923a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2   # The interpreter displays the single-object tuple with the trailing comma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e6dec0f",
   "metadata": {},
   "source": [
    "# 字典\n",
    "\n",
    "> 1.创建字典的三种常见方法\n",
    ">> 1. 直接按照字典的格式创建 : {key1:value1 ,key2:value2 ,.... }\n",
    ">> 2. dict()函数来创建字典:  dict(key1=value1,key2=value2, ....)\n",
    ">> 3. 字典数据增加的方式：新建空字典 dict_name ={} ,dict_name[key1]=value1 ...\n",
    "> 2.字典的性质"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a70d8e3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Mary'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 直接按照字典的格式创建 : {key1:value1 ,key2:value2 ,.... }\n",
    "stud_info = {\n",
    "    \"stud_10001\":\"Alex\",  # : ；  () （） , ，\n",
    "    \"stud_10002\":\"Jack\",\n",
    "    \"stud_10003\":\"Mary\"\n",
    "}\n",
    "stud_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "87b2732d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Mary'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  2. dict()函数来创建字典:  dict(key1=value1,key2=value2, ....)\n",
    "stud_info_02 = dict(\n",
    "    stud_10001 = \"Alex\", \n",
    "    stud_10002 = \"Jack\",\n",
    "    stud_10003 = \"Mary\",\n",
    ")\n",
    "stud_info_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1378aa46",
   "metadata": {},
   "outputs": [],
   "source": [
    "stud_info_03 = {}\n",
    "stud_info_03[\"stud_10001\"] = \"Alex\"\n",
    "stud_info_03[\"stud_10002\"] = \"Jack\"\n",
    "stud_info_03[\"stud_10003\"] = \"Marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2aa8bfca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Marry'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f6be2fe4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['stud_10001', 'stud_10002', 'stud_10003'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7bcf5568",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Alex', 'Jack', 'Marry'])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "af904734",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "96a1ecbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'sape': 4139, 'guido': 4127}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel = {'jack': 4098, 'sape': 4139}\n",
    "tel['guido'] = 4127\n",
    "tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "549e19ef",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4098"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel['jack']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "acf7f69d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'guido': 4127, 'irv': 4127}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del tel['sape']\n",
    "tel['irv'] = 4127\n",
    "tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3608fbee",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'guido', 'irv']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(tel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c651638b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['guido', 'irv', 'jack']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(tel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b63eb255",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'guido' in tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "739a7c65",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'jack' not in tel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a161533",
   "metadata": {},
   "source": [
    "dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "593b1e53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sape': 4139, 'guido': 4127, 'jack': 4098}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "feb51084",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: 4, 4: 16, 6: 36}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{x: x**2 for x in (2, 4, 6)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e0e32da7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sape': 4139, 'guido': 4127, 'jack': 4098}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(sape=4139, guido=4127, jack=4098)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "556014a1",
   "metadata": {},
   "source": [
    "Operation  Result  Notes\n",
    "\n",
    "x or y   if x is false, then y, else x  (1)  it only evaluates the second argument if the first one is false\n",
    "\n",
    "x and y  if x is false, then x, else y  (2)  it only evaluates the second argument if the first one is true\n",
    "\n",
    "not x    if x is false, then True, else False  (3)  not a == b is interpreted as not (a == b), and a == not b is a syntax error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f4c9b2b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:initializa\n",
      "a was found 2 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 4 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}     # Create an empty dictionary\n",
    "  \n",
    "found[\"a\"]=0\n",
    "found[\"e\"]=0\n",
    "found[\"i\"]=0    # Initialize the value associated with each of the keys (each vowel) to 0.\n",
    "found[\"o\"]=0\n",
    "found[\"u\"]=0\n",
    "found = found.fromkeys(vowels,0)\n",
    " \n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1     # Increment the value referred to by “found[letter]” by one\n",
    "for k,v in sorted(found.items()):   # Invoke the “items” method on the “found” dictionary to access each row of data with each iteration.\n",
    "    print(k, 'was found',v, 'time(s).')   # The key and the value are used to create each output message.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "16b6c174",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:tuition\n",
      "i 2\n",
      "o 1\n",
      "u 1\n"
     ]
    }
   ],
   "source": [
    "# 引入setdefault\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "  \n",
    "for letter in word:\n",
    "     if letter in vowels:\n",
    "        found.setdefault(letter,0)  #如果是元音字母，就在字典found中初始化这个元音字母，值为0\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "     print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ebca1949",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels: absent\n",
      "e\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels: \")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ea3a790d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'Arthur': {'Gender': 'Male',\n",
       "  'Home Planet': 'Earth',\n",
       "  'Name': 'Arthur Dent',\n",
       "  'Occupation': 'Sandwich-Maker'},\n",
       " 'Ford': {'Gender': 'Male',\n",
       "  'Home Planet': 'Betelgeuse Seven',\n",
       "  'Name': 'Ford Prefect',\n",
       "  'Occupation': 'Researcher'},\n",
       " 'Robot': {'Gender': 'Unknown',\n",
       "  'Home Planet': 'Unknown',\n",
       "  'Name': 'Marvin',\n",
       "  'Occupation': 'Paranoid Android'},\n",
       " 'Trillian': {'Gender': 'Female',\n",
       "  'Home Planet': 'Earth',\n",
       "  'Name': 'Tricia McMillan',\n",
       "  'Occupation': 'Mathematician'}}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的嵌套/访问一个复杂数据结构的数据\n",
    "import pprint\n",
    "people = {}\n",
    "pprint.pprint(people)\n",
    "{'Arthur': {'Gender': 'Male',\n",
    "            'Home Planet': 'Earth',\n",
    "             'Name': 'Arthur Dent',\n",
    "             'Occupation': 'Sandwich-Maker'},\n",
    "'Ford': {'Gender': 'Male',\n",
    "         'Home Planet': 'Betelgeuse Seven',\n",
    "         'Name': 'Ford Prefect',\n",
    "         'Occupation': 'Researcher'},\n",
    "'Robot': {'Gender': 'Unknown',\n",
    "          'Home Planet': 'Unknown',\n",
    "          'Name': 'Marvin',\n",
    "          'Occupation': 'Paranoid Android'},\n",
    "'Trillian': {'Gender': 'Female',\n",
    "             'Home Planet': 'Earth',\n",
    "             'Name': 'Tricia McMillan',\n",
    "             'Occupation': 'Mathematician'}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "664214b6",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17a00b2b",
   "metadata": {},
   "source": [
    "### Sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2f8cdb18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = { 'a', 'e', 'e', 'i', 'o', 'u', 'u' }\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "45900ce2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2 = set('aeeiouu')\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "679031d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1c1b2826",
   "metadata": {},
   "outputs": [],
   "source": [
    "u = vowels.union(set(word))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "648aaeb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list = sorted(list(u))  # A sorted list of unique letters\n",
    "u_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d7b6db0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "34235220",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2633f937",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels: tush\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')  # Create a set of vowels.\n",
    "word = input(\"Provide a word to search for vowels: \")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4790a4c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pear', 'orange', 'banana', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)                      # show that duplicates have been removed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "4577e85e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'orange' in basket                 # fast membership testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "966c2b1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'crabgrass' in basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ee08d11d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'r'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Demonstrate set operations on unique letters from two words\n",
    "\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "a                                  # unique letters in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "24ca4ece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'r'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a - b     # letters in a but not in b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7ee4f474",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a | b          # letters in a or b or both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "257e33c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'c'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a & b          # letters in both a and b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "68f6380e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ^ b         # letters in a or b but not both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1f26f408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'r'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {x for x in 'abracadabra' if x not in 'abc'}\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65101e62",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
